package com.atguigu.upp.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

/**
 * Created by Smexy on 2022/11/12
 *
 *
 insert into user_tag_value_string
 select
 t.1 tag, t.2 tagValue, bitmapToArray(groupBitmapState(toUInt32(uid))),'2020-06-14'
 from
 (-- 从宽表中，把标签名也作为1列查询出来
 select
 uid,
 arrayJoin(
 [
 ('TAG_POPULATION_ATTRIBUTES_NATURE_GENDER', TAG_POPULATION_ATTRIBUTES_NATURE_GENDER),
 ('TAG_POPULATION_ATTRIBUTES_NATURE_PERIOD', TAG_POPULATION_ATTRIBUTES_NATURE_PERIOD),
 ('TAG_CONSUMING_BEHAVIOR_ORDER_AMOUNT7D', TAG_CONSUMING_BEHAVIOR_ORDER_AMOUNT7D)
 ]
 ) t
 from up_tag_merge_2020_06_14
 ) tmp
 group by t.1, t.2;
 */
public interface ClickhouseMapper
{
    @Update("drop table if exists ${table}")
    void dropTable(@Param("table") String tableName);

    @Update("create table if not exists ${table}(uid String,${column}) engine = MergeTree order by (uid)")
    void createTable(@Param("table") String tableName,@Param("column") String columnStr);

    @Insert("insert into ${table} select t.1 tag, if(t.2 = '','0',t.2) tagValue, groupBitmapState(toUInt64(uid)),#{dt} from " +
            "  ( select uid,arrayJoin( [ ${tagSql}  ] ) t from  ${source} where length(uid) > 0 )tmp  group by t.1, t.2"
    )
    void insertBitmapSql(@Param("table") String table,@Param("dt") String doDate,
                         @Param("tagSql") String tagSql,@Param("source") String sourceTable);

    //考虑幂等性  如果今天的分群数据，写了一般，进程挂掉了，需要重新写，如何保证不重复
    //每次写之前，先把今天要写入的分区的数据删除
    @Update("alter table ${table} delete where dt =#{dt}")
    void deleteBitMapData(@Param("table") String table,@Param("dt") String doDate);
}
